package com.czl.module_service.fragment.approvalFlow

import android.widget.ListView
import androidx.databinding.ViewDataBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.czl.base.base.BaseFragment
import com.czl.base.data.bean.tengyun.TaskUserBean
import com.czl.module_service.view.approvalFlow.ApprovalFlowLayout
import com.czl.module_service.viewmodel.ApprovalFlowViewModel

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
abstract class ApprovalFlowFragment<V : ViewDataBinding, M : ApprovalFlowViewModel> :
    BaseFragment<V, M>() {
    var taskUserId: String? =""
    var businessId: Int =0
    var relationId: Int =0
    val flowLayout: ApprovalFlowLayout by lazy {
        ApprovalFlowLayout(requireContext())
    }

    fun initFlowData(businessId: Int, relationId: Int) {
        initFlowData(businessId, relationId,"")
    }
    fun initFlowData(businessIdTemp: Int, relationIdTemp: Int,taskUserIdTemp: String?) {
        taskUserId = taskUserIdTemp
        businessId = businessIdTemp
        relationId = relationIdTemp
        viewModel.getTaskUserByRelationId(businessId, relationId)
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.changeEvent.loadCompleteEvent.observe(this, {
            if (listView() != null) {
                listView()!!.addFooterView(flowLayout)
            } else {
                getAdapter().addFooterView(flowLayout, addIndex())
            }
            flowLayout.mData = it!!
            getTaskUserId(it)
        })
        viewModel.changeEvent.loadTaskStatusEvent.observe(this, {
            getTaskStatus(it)
        })
    }

    abstract fun getAdapter(): BaseQuickAdapter<*, *>

    open fun listView() : ListView? {
        return null
    }
    open fun addIndex() : Int {
        return 0
    }
    private fun getTaskUserId (list: List<TaskUserBean>?) {
        if(list!=null && !list.isEmpty()){
            val taskUserBean = list[list.size - 1]
            var taskStatus:Int? = 0
            if (taskUserId!= null && taskUserId!!.isNotEmpty()) {
                for (bean in taskUserBean.list!!) {
                    for (bean1 in bean.taskUserList!!) {
                        if (taskUserId.equals(bean1.taskUserId)) {
                            taskStatus = bean1.status!!
                            break
                        }
                    }
                    if (taskStatus != 0) {
                        break
                    }
                }
            }
            var processStatus = taskUserBean.processStatus!!
            dataResult(processStatus,taskStatus)
        } else {
            viewModel.getTaskStatus(businessId, relationId)
        }
    }

    private fun getTaskStatus(data: TaskUserBean?){
        val list = data?.submitList
        if (list!=null && !list.isEmpty()) {
            var json = list[list.size - 1]
            dataResult(json.status, 0)
        } else {
            dataResult(3, 0)
        }
    }
   open fun dataResult(processStatus: Int?,taskStatus: Int?) {}
}